package m.app.action;

import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.store.model.IncomeAmount;
import m.store.model.IncomeAmountRecord;
import m.store.model.OrderModel;
import m.store.service.StoreNumberService;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentParamMeta;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ActionMeta(name = "app/incomeAmount",title = "app出库收款")
public class IncomeAmountAction extends AppAction{
    private IncomeAmount model;
    private List<IncomeAmountRecord> list;

    @DocumentMeta(
        method = @DocumentMethodMeta(title = "我的出库收款登记列表",permission = true),
        params = {
            @DocumentParamMeta(name = "page.index",description = "分页开始位置",type = FieldType.INT),
            @DocumentParamMeta(name = "page.num",description = "分页每页数量",type = FieldType.INT),
            @DocumentParamMeta(name = "params[oid@eq]",description = "条件示例，字段和操作符中间用@，值为空不添加",type = FieldType.STRING,length = 50),
            @DocumentParamMeta(name = "params[oid#desc]",description = "排序示例，字段和操作符中间用#，值为空不生效",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage myList() throws Exception {
        ModelQueryList queryList=toQueryList(IncomeAmount.class,"*","guest.*");
        queryList.addCondition(QueryCondition.eq("inputUser.oid", PowerUtil.getSessionUserOid()));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "出库收款待审核列表",permission = true),
        params = {
            @DocumentParamMeta(name = "page.index",description = "分页开始位置",type = FieldType.INT),
            @DocumentParamMeta(name = "page.num",description = "分页每页数量",type = FieldType.INT),
            @DocumentParamMeta(name = "params[oid@eq]",description = "条件示例，字段和操作符中间用@，值为空不添加",type = FieldType.STRING,length = 50),
            @DocumentParamMeta(name = "params[oid#desc]",description = "排序示例，字段和操作符中间用#，值为空不生效",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage checkList() throws Exception {
        ModelQueryList queryList=toQueryList(IncomeAmount.class,"*","guest.*");
//        queryList.addCondition(QueryCondition.eq("status", OrderModel.Status_Check));
        queryList.addCondition(QueryCondition.eq("store.oid", PowerUtil.getSessionStoreOid()));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "获取出库付款",permission = true),
        params = {
            @DocumentParamMeta(name = "model.oid",description = "主键",type = FieldType.STRING,length = 50,notnull = true)
        }
    )
    public JSONMessage getInfo() throws Exception {
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*","guest.*","inputUser.realname","checkUser.realname");
            PowerUtil.verifyStoreData(model.getStore().getOid());
            list=ModelQueryList.getModelList(IncomeAmountRecord.class,new String[]{"*","sale.*"},null,
                    QueryCondition.eq("income.oid",model.getOid()));
        }else{
            model=new IncomeAmount();
            model.setOrderNo(StoreNumberService.getNextIncomeNumber());
            model.setStatus(OrderModel.Status_Register);
            model.setInputUser(PowerUtil.getSessionUser(true));
            model.setInputDate(new Date());
            model.setIncomeDate(new Date());
            list=new ArrayList<>();
        }
        boolean edit=OrderModel.Status_Register.equals(model.getStatus());
        boolean check=OrderModel.Status_Check.equals(model.getStatus())&&PowerUtil.hasPower(PowerUtil.Finance);
        return JSONMessage.success("查询成功")
                .push("model",model)
                .push("list",list)
                .push("edit",edit)
                .push("check",check);
    }

    public IncomeAmount getModel() {
        return model;
    }

    public void setModel(IncomeAmount model) {
        this.model = model;
    }

    public List<IncomeAmountRecord> getList() {
        return list;
    }

    public void setList(List<IncomeAmountRecord> list) {
        this.list = list;
    }
}
